<?php


namespace app\admin\business;


use app\common\model\ConsignorReplenish;
use app\common\model\LogisticsReplenish;
use app\common\model\Member;
use app\common\model\MemberInfo;
use app\common\model\MemberInfoAddUser;

class memberBus
{
    public $memberMode = null;
    public $memberInfoMode = null;
    public $consignorMode = null;
    public $logisticsMode = null;
    public $memberInfoAddUser = null;
    public function __construct()
    {
        $this->memberMode = new Member();
        $this->memberInfoMode = new MemberInfo();
        $this->consignorMode = new ConsignorReplenish();
        $this->logisticsMode = new LogisticsReplenish();
        $this->memberInfoAddUser = new MemberInfoAddUser();
    }

    /**
     * 获取member以及memberInfo分页信息
     */
    public function getMemberList($phone,$start_time,$end_time,$paging){
        if($end_time!=''&&$start_time!=''){
            $result = $this->memberMode->getListByTime($phone,$start_time,$end_time,$paging,);
        }else{
            $result = $this->memberMode->getList($phone,$paging);
        }
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }

    /**
     * 获取member以及memberInfo分页信息根据member_category
     */
    public function getMemberListCategory($phone,$start_time,$end_time,$paging,$where){
        if($end_time!=''&&$start_time!=''){
            $result = $this->memberMode->getListByCategoryTime($phone,$start_time,$end_time,$paging,$where);
        }else{
            $result = $this->memberMode->getListByCategory($phone,$paging,$where);
        }
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }

    /**
     * 分页获取member的信息
     * @param $current_page
     * @param $num
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMemberPageBus($current_page,$num){
        $result = $this->memberMode->getListPage($current_page,$num);
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function getMemberAllInfoBus($id){

        $result =$this->memberMode->getMemberInfoById($id);
        if($result->memberInfo != null){
            if($result->memberInfo['member_category'] == 1){
                $supplement = $this->consignorMode->getReplenishById(['member_id'=>$id]);
                $result["supplement"] = $supplement;
            }else{
                $supplement = $this->logisticsMode->getReplenishById(['member_id'=>$id]);
                $result["supplement"] = $supplement;
            }
        }else{
            $result["memberInfo"] = [];
            $result["supplement"] = [];
        }
        return $result;
    }
    public function getMemberAllInfoByMemberIdBus($id){
        $where = [
            'member_id'=>$id
        ];
        $result =$this->memberInfoMode->getMemberInfoById($where);
        if(!empty($result)){
            if($result->memberInfo['member_category'] == 1){
                $supplement = $this->consignorMode->getReplenishById(['member_id'=>$id]);
                $result["supplement"] = $supplement;
            }else{
                $supplement = $this->logisticsMode->getReplenishById(['member_id'=>$id]);
                $result["supplement"] = $supplement;
            }
        }else{
            $result["memberInfo"] = [];
            $result["supplement"] = [];
        }
        return $result;
    }
    public function changeMemberInfoStatusBus($where,$data){
        $result =$this->memberInfoMode->changeMemberInfoFiled($where,$data);
        return $result;
    }
    public function removeMemberByIdBus($id){
        //关于会员删除操作，先查出memberInfo,判断刷出哪张补充表
        $resultData = $this->memberMode->getMemberInfoById($id);
        if(!empty($resultData->memberInfo)){
            $resultMemberInfo = $this->memberMode->removeMemberInfo($id);
            if($resultData->memberInfo['member_category']==1){
                $this->consignorMode->removeReplenishByMemberId($id);
            }else{
                $this->logisticsMode->removeReplenishByMemberId($id);
            }
            return $resultMemberInfo;
        }else{
            $resultMemberInfo = $this->memberMode->removeMember($id);
            return $resultMemberInfo;
        }
    }
    public function changeMemberGradeBus($member_id,$member_time){

        $where = [
            'member_id' => $member_id
        ];
        if(strtotime($member_time)<time()){
            throw new \think\Exception('日期必须大于今天');
        }
        $data = [
            'member_time' => $member_time,
            'member_grade' => 2
        ];
        $result = $this->memberInfoMode->changeMemberInfoFiled($where,$data);
        return $result;
    }
    public function getMemberInfoOneByIdBus($member_id){
        $where = [
            'member_id' => $member_id
        ];
        $result = $this->memberInfoMode->getMemberInfoById($where);
        return $result;
    }
    public function removeLogisticsByWhereBus($where){
        $res = $this->memberInfoAddUser->removeByWhere($where);
        if (empty($res)){
            throw new Exception('删除失败');
        }
        return $res;
    }
    //获取member_info_add_user中的数据
    public function getListChildrenPageBus($where,$paging){
        $res = $this->memberInfoAddUser->getListByPage($where,$paging);
        return $res;
    }

    //设置首页显示
    public function setCommendFlagBus($where,$data){
        $res = $this->memberInfoMode->changeMemberInfoFiled($where,$data);
        return $res;
    }
}